package dingding;

import dingding.dao.configInformation;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

// 读取ini配置文件
class INIReader {
    public static Map<String, String> readINIFile(String filePath, configInformation config) {
        Map<String, String> keyValueMap = new HashMap<>();
        String encoding = MyUtils.codeType(filePath);
        encoding = (encoding==null?"UTF-8":encoding);
        try (BufferedReader reader = new BufferedReader(
                new FileReader(filePath, Charset.forName(encoding))
        )) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();

                // Skip empty lines and comments
                if (line.isEmpty() || line.startsWith(";")) {
                    continue;
                }
//                new String(columns[i].getBytes("ISO-8859-1"), "UTF-8");
                // Split line into key-value pair
                int equalsIndex = line.indexOf("=");
                if (equalsIndex != -1) {
                    String key = line.substring(0, equalsIndex).trim();
                    String value = line.substring(equalsIndex + 1).trim();
//                    value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
                    keyValueMap.put(key, value);
                }

            }
            config.setRepon(null);
        } catch (IOException e) {
//            e.printStackTrace();
            config.setRepon("读取配置文件失败！请确保配置文件在同目录下后重启软件，或者直接窗口输入信息后运行！\n");
//            System.out.println("66666读取配置文件失败！");
            return null;
        }

        return keyValueMap;
    }
}

